package com.easyctrl.ldy.view;

import com.easyctrl.fragment.TimerFragmentUser;
import com.easyctrl.ldy.activity.LoginActivity;
import com.easyctrl.ldy.activity.MainApplication;
import com.easyctrl.ldy.activity.R;
import com.easyctrl.ldy.activity.UserActivity;
import com.easyctrl.ui.base.BaseActivity;

import de.greenrobot.event.EventBus;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener; 
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class UserBackView extends RelativeLayout implements OnClickListener {

	private LayoutInflater mInflater;
	private Context mContext;  
	private View view; 
	private UserActivity userActivity;
	private TextView host;
	
	public UserBackView(Context context, AttributeSet attrs) {
		super(context,attrs); 
		this.mContext = context;
		userActivity = (UserActivity) mContext;
		mInflater = LayoutInflater.from(context);
		view = mInflater.inflate(R.layout.user_back_view, this); 
		view.findViewById(R.id.info).setOnClickListener(this);
		view.findViewById(R.id.leftButton).setOnClickListener(this);
		view.findViewById(R.id.rightButton).setOnClickListener(this);
		host = (TextView) view.findViewById(R.id.host);
		host.setText(MainApplication.userManager.currentHost());
	}

	@Override
	public void onClick(View v) { 
		if(v.getId() == R.id.info) {
			Intent intent = new Intent(mContext, LoginActivity.class);
			mContext.startActivity(intent);  
			//if(MainApplication.easySocket != null){
			//	MainApplication.easySocket.close();
			//	MainApplication.easySocket = null;
			//}
		}else if(v.getId() == R.id.leftButton){
			userActivity.popFragments("user", R.id.center_layout, true);  
		}else if(v.getId() == R.id.rightButton){
			TimerFragmentUser timerFragmentUser = new TimerFragmentUser(); 
			EventBus.getDefault().register(timerFragmentUser); 
			userActivity.pushFragments(BaseActivity.USRT, timerFragmentUser, true, true, R.id.center_layout);  
		}
	}  
}
